package com.example.audiostreaming.implement;

import com.example.audiostreaming.data.XimalayApi;
import com.example.audiostreaming.interfaces.ISearchLogic;
import com.example.audiostreaming.interfaces.ISearchViewCallback;
import com.example.audiostreaming.utils.UtilConstants;
import com.example.audiostreaming.utils.UtilLog;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.SearchAlbumList;
import com.ximalaya.ting.android.opensdk.model.word.HotWord;
import com.ximalaya.ting.android.opensdk.model.word.HotWordList;
import com.ximalaya.ting.android.opensdk.model.word.QueryResult;
import com.ximalaya.ting.android.opensdk.model.word.SuggestWords;

import java.util.ArrayList;
import java.util.List;

public class ImplSearchLogic implements ISearchLogic {
    private static final String TAG = "ImplSearchLogic";
    List<Album> mSearchResult = new ArrayList<>();

    private List<ISearchViewCallback> mISearchViewCallbacks = new ArrayList<>();
    private String mCurrentKeyword;//当前搜索关键字
    private  XimalayApi mXimalayApi;
    private static final int DEFAULT_PAGE = 1;
    private int mCurrentPage = DEFAULT_PAGE;

    private ImplSearchLogic (){
        mXimalayApi = XimalayApi.getXimalayApi();
    }

    private static ImplSearchLogic sImplSearchLogic = null;
    public static ImplSearchLogic getImplSearchLogic(){
        if (sImplSearchLogic==null) {
            synchronized (ImplSearchLogic.class){
                if (sImplSearchLogic==null) {
                    sImplSearchLogic = new ImplSearchLogic();
                }
            }
        }

        return sImplSearchLogic;
    }


    @Override
    public void doSearch(String keyword) {
        mCurrentPage = DEFAULT_PAGE;
        mSearchResult.clear();
        this.mCurrentKeyword = keyword;//用于得新搜索，当网络不好的时候 ,用户会点击重新搜索
        search(keyword);
    }

    private void search(String keyword) {
        mXimalayApi.searchByKeyword(keyword, mCurrentPage, new IDataCallBack<SearchAlbumList>() {
            @Override
            public void onSuccess(SearchAlbumList searchAlbumList) {
                if (searchAlbumList != null) {
                    List<Album> albums = searchAlbumList.getAlbums();
                    mSearchResult.addAll(albums);
                    UtilLog.d(TAG,"albums尺寸 ==> "+albums.size());
                    if (mIsLoadMore) {
                        if (mISearchViewCallbacks != null) {
                            for (ISearchViewCallback iSearchViewCallback : mISearchViewCallbacks) {
                                iSearchViewCallback.onLoadMoreResult(mSearchResult,albums.size()!=0);
                            }
                        }
                        mIsLoadMore = false;
                    }else {
                        if (mISearchViewCallbacks != null) {
                            for (ISearchViewCallback iSearchViewCallback : mISearchViewCallbacks) {
                                iSearchViewCallback.onSearchResultLoaded(mSearchResult);
                            }
                        }
                    }
                }else {
                    UtilLog.d(TAG,"searchAlbumList内容为空");
                }

            }

            @Override
            public void onError(int errorCode, String errorMsg) {
                UtilLog.d(TAG,"search错误码 ==> "+errorCode);
                UtilLog.d(TAG,"search错误信息 ==> "+errorMsg);
                for (ISearchViewCallback iSearchViewCallback : mISearchViewCallbacks) {
                    if (mIsLoadMore) {
                        iSearchViewCallback.onLoadMoreResult(mSearchResult,false);
                        mCurrentPage--;
                        mIsLoadMore = false;
                    }else {
                        iSearchViewCallback.onError(errorCode,errorMsg);
                    }
                }
            }
        });
    }

    @Override
    public void reSearch() {
        search(mCurrentKeyword);
    }

    private boolean mIsLoadMore = false;
    @Override
    public void loadMore() {
        //判断有没有必要进行加载更多
        if (mSearchResult.size()< UtilConstants.COUNT_DEFAULT) {
            for (ISearchViewCallback iSearchViewCallback : mISearchViewCallbacks) {
                iSearchViewCallback.onLoadMoreResult(mSearchResult,false);
            }
        } else {
            mIsLoadMore = true;
            mCurrentPage++;
            search(mCurrentKeyword);
        }
    }

    @Override
    public void getHotWord() {
        mXimalayApi.getHotWords(new IDataCallBack<HotWordList>() {
            @Override
            public void onSuccess(HotWordList hotWordList) {
                if (hotWordList != null) {
                    List<HotWord> hotWords = hotWordList.getHotWordList();
                    for (ISearchViewCallback iSearchViewCallback : mISearchViewCallbacks) {
                        iSearchViewCallback.onHotWordLoaded(hotWords);
                    }
                }
                else {
                    UtilLog.d(TAG,"hotWordList内容为空");
                }
            }

            @Override
            public void onError(int errorCode, String errorMsg) {
                UtilLog.d(TAG,"getHotWord错误码 ==> "+errorCode);
                UtilLog.d(TAG,"getHotWord错误信息 ==> "+errorMsg);
            }
        });
    }

    @Override
    public void getSuggestWord(String keyword) {
        mXimalayApi.getSuggestWord(keyword, new IDataCallBack<SuggestWords>() {
            @Override
            public void onSuccess(SuggestWords suggestWords) {
                if (suggestWords != null) {
                    List<QueryResult> suggestKeyWords = suggestWords.getKeyWordList();
                    UtilLog.d(TAG,"suggestKeyWords尺寸 ==> "+suggestKeyWords.size());
                        for (ISearchViewCallback iSearchViewCallback : mISearchViewCallbacks) {
                            iSearchViewCallback.onSuggestWordLoaded(suggestKeyWords);
                        }
                }else {
                    UtilLog.d(TAG,"suggestKeyWords内容为空");
                }
            }

            @Override
            public void onError(int errorCode, String errorMsg) {
                UtilLog.d(TAG,"getSuggestWord错误码 ==> "+errorCode);
                UtilLog.d(TAG,"getSuggestWord错误信息 ==> "+errorMsg);
            }
        });
    }

    @Override
    public void registerViewCallback(ISearchViewCallback iSearchViewCallback) {
        if (mISearchViewCallbacks != null&&!mISearchViewCallbacks.contains(iSearchViewCallback)) {   //contains,包含
            mISearchViewCallbacks.add(iSearchViewCallback);
        }
    }

    @Override
    public void unRegisterViewCallback(ISearchViewCallback iSearchViewCallback) {
        if (mISearchViewCallbacks != null) {
            mISearchViewCallbacks.remove(iSearchViewCallback);
        }
    }
}
